﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/24 9:19:20------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;
using Newtonsoft.Json;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class action_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected int hidConditions = 0;
        protected int hidCoupons_form = 0;
        protected int hidExpiration_form = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.actions().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                ChkAdminLevel("actions", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }

                txtStart_time.Text = txtStart_time.Text.Trim() == "" ? DateTime.Now.ToString("yyyy-MM-dd") : txtStart_time.Text.Trim();
                txtEnd_time.Text = txtEnd_time.Text.Trim() == "" ? DateTime.Now.AddDays(7).ToString("yyyy-MM-dd") : txtEnd_time.Text.Trim();
            }

        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.actions bll = new BLL.actions();
            Model.actions model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            txtContents.Text = model.contents;
            txtStart_time.Text = DateTime.Parse(model.start_time.ToString()).ToString("yyyy-MM-dd");
            txtEnd_time.Text = DateTime.Parse(model.end_time.ToString()).ToString("yyyy-MM-dd");
            cbStatus.Checked = model.status == 1 ? true : false;

            txtConditions_amount.Text = model.conditions_amount.ToString();
            txtSort_id.Text = model.sort_id.ToString();
            cblExpiration_form.Text = model.expiration_form.ToString();
            rbtCategory.SelectedValue = model.category.ToString();

            hidExpiration_form = model.expiration_form;

            string idv = string.Empty;
            foreach (Model.action_coupons _coupon in model.coupons)
            {
                idv += _coupon.id + "_" + _coupon.quantity + ",";
            }
            if (idv != "")
            {
                idv = idv.Substring(0, idv.Length - 1);
            }
            Utils.WriteCookie("idv", idv);

            rptList.DataSource = model.coupons;
            rptList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.actions model = new Model.actions();
            BLL.actions bll = new BLL.actions();

            model.title = txtTitle.Text;
            model.contents = txtContents.Text;
            model.start_time = DateTime.Parse(txtStart_time.Text);
            model.end_time = DateTime.Parse(txtEnd_time.Text);
            model.status = cbStatus.Checked ? 1 : 0;

            model.sort_id = int.Parse(txtSort_id.Text);
            model.add_time = DateTime.Now;
            model.expiration_form = int.Parse(cblExpiration_form.SelectedValue);
            model.conditions_amount = decimal.Parse(txtConditions_amount.Text);
            model.action_type = 1;
            model.category = int.Parse(rbtCategory.SelectedValue);

            #region 保存优惠卷====================
            string[] hidCouponsId = Request.Form.GetValues("hidCouponsId");
            string[] quantity = Request.Form.GetValues("quantity");

            if (hidCouponsId != null && hidCouponsId.Length > 0)
            {
                List<Model.action_coupons> ls = new List<Model.action_coupons>();
                for (int i = 0; i < hidCouponsId.Length; i++)
                {
                    ls.Add(new Model.action_coupons
                    {
                        coupon_id = Utils.ObjToInt(hidCouponsId[i]),
                        quantity = Utils.ObjToInt(quantity[i])
                    });

                }
                model.coupons = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加优惠活动 " + JsonConvert.SerializeObject(model)); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.actions bll = new BLL.actions();
            Model.actions model = bll.GetModel(_id);

            model.title = txtTitle.Text;
            model.contents = txtContents.Text;
            model.start_time = DateTime.Parse(txtStart_time.Text);
            model.end_time = DateTime.Parse(txtEnd_time.Text);
            model.status = cbStatus.Checked ? 1 : 0;

            model.sort_id = int.Parse(txtSort_id.Text);
            model.expiration_form = int.Parse(cblExpiration_form.SelectedValue);
            model.conditions_amount = decimal.Parse(txtConditions_amount.Text);
            model.action_type = model.action_type;
            model.category = int.Parse(rbtCategory.SelectedValue);

            #region 保存优惠卷====================
            string[] hidCouponsId = Request.Form.GetValues("hidCouponsId");
            string[] quantity = Request.Form.GetValues("quantity");

            if (hidCouponsId != null && hidCouponsId.Length > 0)
            {
                List<Model.action_coupons> ls = new List<Model.action_coupons>();
                for (int i = 0; i < hidCouponsId.Length; i++)
                {
                    ls.Add(new Model.action_coupons
                    {
                        action_id = model.id,
                        coupon_id = Utils.ObjToInt(hidCouponsId[i]),
                        quantity = Utils.ObjToInt(quantity[i])
                    });

                }
                model.coupons = ls;
            }
            #endregion

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改优惠活动 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("actions", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改优惠活动成功！", "action_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("actions", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加优惠活动成功！", "action_list.aspx");
            }
        }

    }
}

